<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;

class ResponseMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param Closure(Request): (Response) $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $response = $next($request);
//        $contentType = $response->headers->get('content-type', '');
//        $data = [
//            'url'       => $request->getUri(),
//            'method'    => $request->getMethod(),
//            'ip'        => $request->getClientIp(),
//            'query'     => $request->query->all(),
//            'param'     => $request->request->all(),
//            'header'    => $request->headers->all(),
//            'status'    => $response->getStatusCode(),
//        ];
//        // 只保存json返回的
//        if (str_contains($contentType, 'json')) {
//            $data['response'] = $response->getContent();
//        } else {
//            $data['response'] = mb_substr($response->getContent(),0, 1000);
//        }
//        Log::channel('request')->info('request', $data);

        return $response;
    }
}
